import { Provide, Inject } from '@midwayjs/decorator';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository, In } from 'typeorm';
import { DynamicEntity } from '@/entity/dynamic';
import { FriendEntity } from '@/entity/friend';

import { DynamicDTO } from '../dto/dynamic';

import { message, TokenUtil } from '@/util/index';

@Provide()
export class DynamicService {
  @InjectEntityModel(DynamicEntity)
  dynamicEntity: Repository<DynamicEntity>;

  @InjectEntityModel(FriendEntity)
  friendEntity: Repository<FriendEntity>;

  @Inject()
  tokenUtil: TokenUtil;

  /**
   * 发动态
   */
  async sendDynamic(options: DynamicDTO) {
    const userToken: any = await this.tokenUtil.verifyToken();
    const params = {
      ...options,
      dynamicId: userToken.payload.userId,
    };
    const result = await this.dynamicEntity.save(params);
    return message.success(200, '发布成功 ', result);
  }

  /**
   * 获取全部动态
   */
  async getAllDynamic() {
    const userToken: any = await this.tokenUtil.verifyToken();
    const userId = userToken.payload.userId;
    const arrData: any = [userId];
    const friendData = await this.friendEntity.find({
      where: [{ userId }, { friendId: userId }],
    });

    friendData.forEach(item => {
      console.log('item', item);
      arrData.push(item.userId);
      arrData.push(item.friendId);
    });
    console.log('userId', userId);
    const formatData = [];
    const result = await this.dynamicEntity.find({
      where: {
        dynamicId: In(arrData),
      },
      relations: ['users'],
      order: {
        id: 'DESC',
      },
      // select: ['id', 'username', 'imgUrl', 'desc'],
    });
    result.forEach(item => {
      console.log('item :', item);
      if (item.dynamicId === userId) {
        formatData.push({
          type: 1,
          id: item.id,
          dynamicId: item.dynamicId,
          content: item.content,
          imgUrl: item.imgUrl,
          status: item.status,
          createTime: item.createTime,
          name: item.users.username,
          userImage: item.users.imgUrl,
        });
      } else {
        formatData.push({
          type: 2,
          id: item.id,
          dynamicId: item.dynamicId,
          content: item.content,
          imgUrl: item.imgUrl,
          status: item.status,
          createTime: item.createTime,
          name: item.users.username,
          userImage: item.users.imgUrl,
        });
      }
    });
    return message.success(200, '获取动态列表', formatData);
  }

  /**
   * 获取自己或别人动态
   */
  async getDynamic(id: number) {
    const result = await this.dynamicEntity.find({ where: { dynamicId: id } });
    return message.success(200, '获取成功', result);
  }

  /**
   * 删除动态
   */
  async deleteDynamic(id: number) {
    // console.log('删除动态', id);
    const result = await this.dynamicEntity
      .createQueryBuilder()
      .delete()
      .where({ id })
      .execute();
    return message.success(200, '删除成功', result);
  }

  /**
   * 动态点赞
   */
  async dynamicStar() { }

  /**
   * 动态评论
   */
  async dynamicComment() { }

  /**
   * 获取动态评论
   */
  async getDynamicComment() { }
}
